package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysUserComm;
import com.ruoyi.system.service.ISysUserCommService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户和社区关联Controller
 * 
 * @author ruoyi
 * @date 2021-08-29
 */
@RestController
@RequestMapping("/system/apply")
public class SysUserCommController extends BaseController
{
    @Autowired
    private ISysUserCommService sysUserCommService;

    /**
     * 查询当前社区的申请列表列表
     */
    @PreAuthorize("@ss.hasPermi('system:apply:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysUserComm sysUserComm)
    {
        startPage();
        sysUserComm.setCommunityId(SecurityUtils.getCommunityId());
        List<SysUserComm> list = sysUserCommService.selectSysUserCommList(sysUserComm);
        return getDataTable(list);
    }

    /**
     * 查询当前用户的申请列表列表
     */
    @GetMapping("/list/current")
    public TableDataInfo listCurrentUser(SysUserComm sysUserComm)
    {
        sysUserComm.setUserId(SecurityUtils.getUserID());
        List<SysUserComm> list = sysUserCommService.selectSysUserCommList(sysUserComm);
        return getDataTable(list);
    }

    /**
     * 导出用户和社区关联列表
     */
    @PreAuthorize("@ss.hasPermi('system:apply:export')")
    @Log(title = "用户和社区关联", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysUserComm sysUserComm)
    {
        List<SysUserComm> list = sysUserCommService.selectSysUserCommList(sysUserComm);
        ExcelUtil<SysUserComm> util = new ExcelUtil<SysUserComm>(SysUserComm.class);
        return util.exportExcel(list, "用户和社区关联数据");
    }

    /**
     * 获取用户和社区关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:apply:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysUserCommService.selectSysUserCommById(id));
    }

    /**
     * 新增用户和社区关联
     */
    @Log(title = "用户和社区关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysUserComm sysUserComm)
    {
        // 若 userId 为空则是当前用户自己申请的，添加上 userId
        if (null == sysUserComm.getUserId() || sysUserComm.getUserId() == 0) {
            sysUserComm.setUserId(SecurityUtils.getUserID());
        }
        // 添加状态为 0
        sysUserComm.setStatus("0");
        return toAjax(sysUserCommService.insertSysUserComm(sysUserComm));
    }

    /**
     * 修改用户和社区关联
     */
    @PreAuthorize("@ss.hasPermi('system:apply:edit')")
    @Log(title = "用户和社区关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysUserComm sysUserComm)
    {
        return toAjax(sysUserCommService.updateSysUserComm(sysUserComm));
    }

    /**
     * 删除用户和社区关联
     */
    @PreAuthorize("@ss.hasPermi('system:apply:remove')")
    @Log(title = "用户和社区关联", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysUserCommService.deleteSysUserCommByIds(ids));
    }

    @GetMapping("/getCheckedComs")
    public AjaxResult getCheckedComs(){
        Long userId = SecurityUtils.getUserID();
        AjaxResult ajax = AjaxResult.success();
        List<SysUserComm> res = sysUserCommService.getCheckedComs(userId);
        ajax.put("data",res);
        return ajax;
    }

    @GetMapping("/getUnregisterComs")
    public AjaxResult getUnregisterComs(){
        Long userId = SecurityUtils.getUserID();
        AjaxResult ajax = AjaxResult.success();
        List<SysUserComm> res = sysUserCommService.getUnregisterComs(userId);
        ajax.put("data",res);
        return ajax;
    }
}
